package com.example.musicplayer;

import android.content.Context;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.LocalMusicViewHolder>{
    Context context;
    List<MusicBean>mDatas; //数据源

    OnItemClickListener onItemClickListener;    //通过函数传入接口


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    //设置接口
    public interface OnItemClickListener{
        public void OnItemClick(View view, int position);   //传递点击的view，以及位置
    }

    //构造函数
    public MusicAdapter(Context context, List<MusicBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @NonNull
    @Override
    //创建并返回ViewHolder
    public LocalMusicViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //将布局转换为view对象
        View view = LayoutInflater.from(context).inflate(R.layout.item_music,parent,false);
        LocalMusicViewHolder holder = new LocalMusicViewHolder(view); //将view传入ViewHolder
        return holder;
    }

    @Override
    //绑定iewHolder
    public void onBindViewHolder(@NonNull LocalMusicViewHolder holder, final int position) {
        MusicBean musicBean = mDatas.get(position);//获取集合指定的数据
        //将数据set到控件中
        holder.songTv.setText(musicBean.getSong());
        holder.singerTv.setText(musicBean.getSinger());
        holder.albumTv.setText(musicBean.getAlbum());
        holder.timeTv.setText(musicBean.getDuration());
        //设置封面，找不到封面时自动使用默认封面填充
        String albumArt = musicBean.getAlbumArt();
        Bitmap bm = BitmapFactory.decodeFile(albumArt); //解析成图片
        if (bm == null){
            bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
        }
        holder.albumIv.setImageBitmap(bm);
//      获取每个列表，设置点击事件，用接口回调方式（当点击了某向，就会调用接口中的OnItemClick）
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.OnItemClick(v,position);    //接口回调
            }
        });
    }

    @Override
    //返回条目（即集合中有多少数据，返回多少数据）
    public int getItemCount() {
        return mDatas.size();
    }
    //内部类，继承RecyclerView.ViewHolder
    class LocalMusicViewHolder extends RecyclerView.ViewHolder{
        TextView songTv,singerTv,albumTv,timeTv;
        ImageView albumIv;
        //初始化控件
        public LocalMusicViewHolder(View itemView) {
            super(itemView);
            albumIv = itemView.findViewById(R.id.item_local_music_num);     //专辑图片
            songTv = itemView.findViewById(R.id.item_local_music_song);     //歌曲名称
            singerTv = itemView.findViewById(R.id.item_local_music_singer);//歌手名称
            albumTv = itemView.findViewById(R.id.item_local_music_album);   //专辑名称
            timeTv = itemView.findViewById(R.id.item_local_music_durtion);  //时间
        }
    }
}
